package com.example.demo.yjcx.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.yjcx.FeeItemTypeEnum;
import com.example.demo.yjcx.dao.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author linjingze
 * @date 2023/12/21 3:45 PM
 */

@Service
public class AreaFeeItemService {

    @Autowired
    private AreaFeeItemMapper areaFeeItemMapper;

    @Autowired
    private WarehouseFeeRelMapper warehouseFeeRelMapper;

    @Autowired
    private WarehouseMapper warehouseMapper;


    public List<AreaFeeItem> listFeeItemByWarehouse(String warehouse) {
        QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", warehouse);
        List<Warehouse> warehouseList = warehouseMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(warehouseList)) {
            return Collections.emptyList();
        }
        List<Integer> wareHouseIdList = warehouseList.stream().map(Warehouse::getId).toList();
        QueryWrapper<WarehouseFeeRel> relQueryWrapper = new QueryWrapper<>();
        relQueryWrapper.in("warehouse_id", wareHouseIdList);
        List<WarehouseFeeRel> relList = warehouseFeeRelMapper.selectList(relQueryWrapper);
        List<Integer> feeItemIdIdList = relList.stream().map(WarehouseFeeRel::getFeeItemId).toList();
        List<AreaFeeItem> areaFeeItems = areaFeeItemMapper.selectBatchIds(feeItemIdIdList);
        List<String> areaList = areaFeeItems.stream().map(AreaFeeItem::getArea).distinct().toList();
        List<AreaFeeItem> qhdFeeList = listFeeItemByArea(areaList);
        List<AreaFeeItem> allFeeList = new ArrayList<>(areaFeeItems);
        allFeeList.addAll(qhdFeeList);
        return allFeeList;
    }

    public List<AreaFeeItem> listFeeItemByArea(List<String> areaList) {
        QueryWrapper<AreaFeeItem> aQ = new QueryWrapper<>();
        aQ.in("area", areaList);
        aQ.in("type", Arrays.asList(FeeItemTypeEnum.MDG.name(), FeeItemTypeEnum.QYG.name()));
        List<AreaFeeItem> qhdFeeList = areaFeeItemMapper.selectList(aQ);
        return qhdFeeList;
    }
}
